package com.changjiang.luban.dao.sys;

import com.changjiang.luban.dao.domain.sys.SysPermission;
import com.changjiang.luban.utils.dao.BaseDao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 菜单持久层接口
 *
 * @author nichangjiang
 * @since 2017/5/29
 */
@Repository
public interface SysPermissionDao extends BaseDao<SysPermission, Long> {

    /**
     * 查询用户权限菜单
     * @param userId 用户编号
     * @return 权限菜单集合
     */
    @Query("select distinct p from SysPermission p join fetch p.roles role join role.users user where " +
            " user.id = :userId order by p.seq asc")
    List<SysPermission> findByUserId(@Param("userId") Long userId);

    /**
     * 查询角色权限菜单
     * @param roleId 角色编号
     * @return 权限菜单集合
     */
    @Query("select distinct p from SysPermission p join fetch p.roles role where " +
            " role.id = :roleId order by p.seq asc")
    List<SysPermission> findByRoleId(@Param("roleId") Long roleId);


    @Query("select distinct p from SysPermission p order by p.seq asc")

    List<SysPermission> queryAll();

    /**
     * 资源分页查询
     *
     * @param pageable 分页参数
     * @return
     */
    @Override
    Page<SysPermission> findAll(Pageable pageable);

}
